package com.sse.comunicacion;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

import com.sse.util.ByteUtil;
import com.sse.util.TramaUtil;

public class Cliente {

	private DataOutputStream dataOutputStream;

	Socket skCliente = null;

	public Cliente(String servidor, int puerto) {

		try {
			skCliente = new Socket(servidor, puerto);
			dataOutputStream = new DataOutputStream(skCliente.getOutputStream());

		} catch (Exception e) {
			e.printStackTrace();
			new RuntimeException(e);
		}
	}

	public void enviarByteAData(byte[] array) throws IOException {
		dataOutputStream.write(array, 0, array.length);
	}

	public void cerrar() throws IOException {
		skCliente.close();
	}

	public static void main(String[] args) throws IOException,
			InterruptedException {

		// long[] data = new long[100000000];
		//
		// for (int i = 0; i < 100; i++) {
		// data[i] = i*10;
		// }

		for (int k = 0; k < 1; k++) {
			new Thread(new Runnable() {
				public void run() {
					try {
						Cliente c = new Cliente("localhost", 5000);
						for (int i = 1; i < 101; i++) {

							long trama = TramaUtil.convertirValoresATrama(i, 0,
									i % 2, i % 8, 21 * i);

							byte[] bTrama = ByteUtil.longToByteA(trama);

							byte[] data = new byte[16];

							for (int j = 0; j < 8; j++) {
								data[j] = bTrama[j];
							}

							long longDate = System.currentTimeMillis();
							byte[] hora = ByteUtil.longToByteA(longDate);

							for (int j = 0; j < 8; j++) {
								data[j + 8] = hora[j];
							}

							// Thread.sleep(300);

							c.enviarByteAData(data);

							// c.enviarStringData(String.valueOf(data[i]));
							// System.out.println("Enviado: "+data+
							// " - Fecha: "+longDate);
						}
						c.cerrar();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}).start();
		}

	}

}
